//BookServlet.java
package com.ylaihui.web;

import com.ylaihui.pojo.Book;
import com.ylaihui.pojo.Page;
import com.ylaihui.service.BookService;
import com.ylaihui.service.impl.BookServiceImpl;
import com.ylaihui.utils.WebUtils;
import jdk.nashorn.internal.ir.RuntimeNode;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pageNo = req.getParameter("pageNo");
        int pageNum = WebUtils.parseInt(pageNo, 0);
        pageNum += 1;

        //1.获取请求参数
        Map<String, String[]> parameterMap = req.getParameterMap();
        //2. 转换为JavaBean 对象 Book 对象
        Book book = WebUtils.copyMapToBean(parameterMap, new Book());
        //3.调用service 的 add 添加对象
        bookService.addBook(book);
        System.out.println(book.getName());
        //4. 请求转发 到列表展示 ， 这种方法， 如果用户按下 F5，会导致表单的重复提交，这里改为请求重定向。
        //req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req, resp);

        //请求重定向, 用户按下 F5，不会导致 表单重复提交。 因为此时浏览器的地址已经改了，不再是原来的添加图书的地址了。
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNum);
    }

    /**
     * 处理分页请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取请求参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        //int pageNo = 1;
        //int pageSize = 4;
        //2. 调用 Service 层的page 方法 查询数据
        Page<Book> page = bookService.page(pageNo, pageSize);

        page.setUrl("manager/bookServlet?action=page");

        //3. 将数据保存到 request域中
        req.setAttribute("page", page);

        //4. 请求转发
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);

        //1. 获取请求参数
        String id = req.getParameter("id");
        int i = WebUtils.parseInt(id, 0);

        //2. 调用 bookService delete 删除信息
        bookService.deleteBookById(i);

        //3. 重定向到商品类列表， 刷新页面
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 0);

        //1. 修改商品信息
        Book book = WebUtils.copyMapToBean(req.getParameterMap(), new Book());
        bookService.updateBook(book);
        //2. 请求重定向
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }
    void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取id
        String id = req.getParameter("id");
        //2. 获取商品信息
        int i = WebUtils.parseInt(id, 0);
        Book book = bookService.queryBookById(i);
        //3. 将商品信息放到req域中
        req.setAttribute("books",book);
        //4. 请求转发
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 通过service 层获取图书列表
        List<Book> books = bookService.queryBooks();
        // 2. 将图书信息放到 request 域中
        req.setAttribute("books", books);
        // 3. 请求转发
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }
}
